package com.lj.cloudDisk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lj.cloudDisk.config.exception.SpaceException;
import com.lj.cloudDisk.mapper.UcenterMemberMapper;
import com.lj.cloudDisk.pojo.UcenterMember;
import com.lj.cloudDisk.pojo.dao.RegisterVo;
import com.lj.cloudDisk.pojo.dao.UcenterMemberDao;
import com.lj.cloudDisk.service.CheckCodeService;
import com.lj.cloudDisk.service.UcenterMemberService;
import com.lj.cloudDisk.utils.JwtUtils;
import com.lj.cloudDisk.utils.MD5;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.security.sasl.SaslException;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户信息
 */
@Service
@Slf4j
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    UcenterMemberMapper ucenterMemberMapper;

    @Resource(name = "PicCheckCodeService")
    private CheckCodeService picCheckCodeService;

    /**
     * 登录验证 加 token的生成
     * @param ucenterMember
     * @return
     */
    @Override
    public Map<String, Object> login(UcenterMemberDao ucenterMember) {
        String code = ucenterMember.getCode();
        String codeKey = ucenterMember.getCodeKey();
        log.debug("验证:{}",code);
        log.debug("验证码key:{}",codeKey);
        //做非空判断
        if(StringUtils.isEmpty(codeKey) || StringUtils.isEmpty(code)){
            throw  new SpaceException(20001,"验证码不能为空");
        }
        //判断验证码是否正确
        boolean isSuccess = picCheckCodeService.verify(codeKey, code);
        if(!isSuccess){
            throw new SpaceException(20001,"验证码错误");
        }

        String mobile = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();

        //做非空判断
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
                 throw  new SpaceException(20001,"手机号或密码为空,登录失败");
        }

        //判断手机号是否注册
        LambdaQueryWrapper<UcenterMember> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(UcenterMember::getMobile, mobile);
        UcenterMember member = ucenterMemberMapper.selectOne(queryWrapper);
        if(member == null){
            throw new SpaceException(20001,"该用户未注册");
        }

        //判断密码

        if(!MD5.encrypt(password).equals(member.getPassword())){
            throw  new SpaceException(20001,"密码错误");
        }

        //生成token
        String jwtToken = JwtUtils.getJwtToken(member.getId(),member.getNickname());

        Map<String, Object> map= new HashMap<>();
        map.put("token",jwtToken);
        map.put("member",member);

        return map;
    }

    /**
     * 注册
     * @param registerVo
     */
    @Override
    @Transactional
    public void register(RegisterVo registerVo) {

        String SMSCode = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();

        //注册开始设置默认头像
        String avatar = registerVo.getAvatar();

        //判断验证码
        String redisCode = (String) redisTemplate.opsForValue().get(mobile);
        if(!redisCode.equals(SMSCode)){
            throw new SpaceException(20001,"短信验证码错误");
        }

        Integer userCount = ucenterMemberMapper.userCount(mobile);
        if(userCount > 0){
           throw  new SpaceException(20001,"用户已注册,请去登录");
        }

        //将数据添加至数据库
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setAvatar(avatar);
        member.setPassword(MD5.encrypt(password));
        member.setNickname(nickname);
        try {
            ucenterMemberMapper.insert(member);
        } catch (Exception e) {
            throw new SpaceException(20001,"注册失败,未知错误");

        }

    }

    /**
     * 查询用户信息
     * @param id
     * @return
     */
    @Override
    public UcenterMember getUserInfo(String id) {
        LambdaQueryWrapper<UcenterMember> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(UcenterMember::getId,id);
        UcenterMember member = ucenterMemberMapper.selectOne(queryWrapper);
        return member;
    }
}
